package cn.daisy.equipment.io.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.daisy.equipment.io.domain.EquIoDetail;
import cn.daisy.equipment.io.mapper.EquIoMapper;
import cn.daisy.equipment.io.domain.EquIo;
import cn.daisy.equipment.io.service.IEquIoService;

/**
 * 业务主表Service业务层处理
 * 
 * @author Kevin
 * @date 2022-10-11
 */
@Service
public class EquIoServiceImpl implements IEquIoService 
{
    @Autowired
    private EquIoMapper equIoMapper;

    /**
     * 查询业务主表
     * 
     * @param id 业务主表主键
     * @return 业务主表
     */
    @Override
    public EquIo selectEquIoById(Long id)
    {
        return equIoMapper.selectEquIoById(id);
    }

    /**
     * 查询业务主表列表
     * 
     * @param equIo 业务主表
     * @return 业务主表
     */
    @Override
    public List<EquIo> selectEquIoList(EquIo equIo)
    {
        return equIoMapper.selectEquIoList(equIo);
    }

    /**
     * 新增业务主表
     * 
     * @param equIo 业务主表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEquIo(EquIo equIo)
    {
        equIo.setCreateTime(DateUtils.getNowDate());
        int rows = equIoMapper.insertEquIo(equIo);
        insertEquIoDetail(equIo);
        return rows;
    }

    /**
     * 修改业务主表
     * 
     * @param equIo 业务主表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEquIo(EquIo equIo)
    {
        equIo.setUpdateTime(DateUtils.getNowDate());
        equIoMapper.deleteEquIoDetailByIoId(equIo.getId());
        insertEquIoDetail(equIo);
        return equIoMapper.updateEquIo(equIo);
    }

    /**
     * 批量删除业务主表
     * 
     * @param ids 需要删除的业务主表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEquIoByIds(Long[] ids)
    {
        equIoMapper.deleteEquIoDetailByIoIds(ids);
        return equIoMapper.deleteEquIoByIds(ids);
    }

    /**
     * 删除业务主表信息
     * 
     * @param id 业务主表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEquIoById(Long id)
    {
        equIoMapper.deleteEquIoDetailByIoId(id);
        return equIoMapper.deleteEquIoById(id);
    }

    /**
     * 新增业务明细信息
     * 
     * @param equIo 业务主表对象
     */
    public void insertEquIoDetail(EquIo equIo)
    {
        List<EquIoDetail> equIoDetailList = equIo.getEquIoDetailList();
        Long id = equIo.getId();
        if (StringUtils.isNotNull(equIoDetailList))
        {
            List<EquIoDetail> list = new ArrayList<EquIoDetail>();
            for (EquIoDetail equIoDetail : equIoDetailList)
            {
                equIoDetail.setIoId(id);
                list.add(equIoDetail);
            }
            if (list.size() > 0)
            {
                equIoMapper.batchEquIoDetail(list);
            }
        }
    }
}
